package com.rxh.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.rxh.mapper.core.*;
import com.rxh.mapper.merchant.MerchantGoodsMapper;
import com.rxh.mapper.merchant.MerchantInfoMapper;
import com.rxh.mapper.merchant.MerchantSettingMapper;
import com.rxh.mapper.merchant.MerchantWebSiteMapper;
import com.rxh.mapper.risk.RiskOrderTrackMapper;
import com.rxh.mapper.sys.SysConstantMapper;
import com.rxh.pojo.base.Page;
import com.rxh.pojo.base.PageResult;
import com.rxh.pojo.base.SearchInfo;
import com.rxh.pojo.core.*;
import com.rxh.pojo.merchant.MerchantInfo;
import com.rxh.pojo.merchant.MerchantSetting;
import com.rxh.pojo.merchant.MerchantWebSite;
import com.rxh.service.AcquirerService;
import com.rxh.service.ConstantService;
import com.rxh.service.MerchantService;
import com.rxh.service.TransactionService;
import com.rxh.utils.DateUtils;
import com.rxh.utils.SystemConstant;
import com.rxh.utils.UUID;
import com.rxh.vo.VoCoreOrder;
import com.rxh.vo.VoCoreOrderChange;
import com.rxh.vo.VoCoreOrderExport;
import com.rxh.vo.VoRiskOrderTrack;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class TransactionServiceImpl implements TransactionService {
    @Resource
    private CoreOrderMapper orderMapper;
    @Resource
    private MerchantInfoMapper merchantInfoMapper;
    @Resource
    private MerchantWebSiteMapper merchantWebSiteMapper;
    @Resource
    private SysConstantMapper constantMapper;
    @Resource
    private CoreAcquirerAccountMapper acquirerAccountMapper;
    @Resource
    private CoreOrderDetailMapper orderDetailMapper;
    @Resource
    private CoreCardholderMapper cardholderMapper;
    @Resource
    private CoreCargoTrackMapper cargoTrackMapper;
    @Resource
    private MerchantGoodsMapper merchantGoodsMapper;
    @Resource
    private CoreOrderGoodsMapper orderGoodsMapper;
    @Resource
    private CoreOrderChangeMapper orderChangeMapper;
    @Resource
    private CoreMerchantOrderMapper merchantOrderMapper;
    @Resource
    private CoreMerchantOrderRmbMapper merchantOrderRmbMapper;
    @Resource
    private MerchantSettingMapper merchantSettingMapper;
    @Resource
    private CoreTradeRateMapper tradeRateMapper;
    @Resource
    private ChannelOrderInfoMapper channelOrderInfoMapper;
    @Resource
    private RiskOrderTrackMapper riskOrderTrackMapper;
    @Resource
    private ConstantService constantService;
    @Resource
    private MerchantService merchantService;
    @Resource
    private AcquirerService acquirerService;


    /**
     * 获取分页数据
     *
     * @param page 分页信息
     * @return 分页数据
     */
    @Override
    public PageResult getOrderList(Page page) {
        // 设置分页信息
        PageHelper.startPage(page.getPageNum(), page.getPageSize());
        if (page.getOrderBy() == null || page.getOrderBy().size() == 0) {
            PageHelper.orderBy("id DESC");
        } else {
            page.getOrderBy().forEach((key, value) -> PageHelper.orderBy(key + " " + value));
        }
        // 执行查询
        if (page.getSearchInfo() != null && page.getSearchInfo().getEndDate() != null) {
            page.getSearchInfo().setEndDate(DateUtils.setEndDate(page.getSearchInfo().getEndDate()));
        }
        List<VoCoreOrder> list = orderMapper.selectOrderBySearchInfo(page.getSearchInfo());
        PageInfo<VoCoreOrder> pageInfo = new PageInfo<>(list);
        // 获取查询结果
        PageResult result = new PageResult();
        result.setRows(list);
        result.setTotal(pageInfo.getTotal());
        result.setAllPage(pageInfo.getPages());
        // 返回结果
        return result;
    }

    @Override
    public CoreOrder selectByMerchantIdAndMerchantOrderId(Integer merchantId, String merchantOrderId) {
        return orderMapper.selectByMerchantIdAndMerchantOrderId(merchantId, merchantOrderId);
    }

    @Override
    public Map<String, Object> getTransStatistics(SearchInfo searchInfo) {
        // 获取统计数据
        Map<String, Object> statistics = new HashMap<>();
        statistics.put("orderStatistics", orderMapper.selectOrderStatistics(searchInfo));
        statistics.put("totalCurrency", orderMapper.selectCurrencyStatistics(searchInfo));
        return statistics;
    }

    @Override
    public PageResult getMerchantOrderList(Page page) {
        // 设置分页信息
        PageHelper.startPage(page.getPageNum(), page.getPageSize());
        if (page.getOrderBy() == null || page.getOrderBy().size() == 0) {
            PageHelper.orderBy("id DESC");
        } else {
            page.getOrderBy().forEach((key, value) -> PageHelper.orderBy(key + " " + value));
        }
        // 执行查询
        if (page.getSearchInfo() != null && page.getSearchInfo().getEndDate() != null) {
            page.getSearchInfo().setEndDate(DateUtils.setEndDate(page.getSearchInfo().getEndDate()));
        }
        List<CoreOrder> list = orderMapper.selectMerchantOrderBySearchInfo(page.getSearchInfo());
        PageInfo<CoreOrder> pageInfo = new PageInfo<>(list);
        // 获取查询结果
        PageResult result = new PageResult();
        result.setRows(list);
        result.setTotal(pageInfo.getTotal());
        result.setAllPage(pageInfo.getPages());
        // 返回结果
        return result;
    }

    @Override
    public Map<String, Object> getMerchantTransStatistics(SearchInfo searchInfo) {
        // 获取统计数据
        Map<String, Object> statistics = new HashMap<>();
        statistics.put("orderStatistics", orderMapper.selectOrderStatistics(searchInfo));
        statistics.put("orderChangeStatistics", orderChangeMapper.selectStatisticsBySearchInfo(searchInfo));
        return statistics;
    }

    @Override
    public Map<String, Object> getBondStatistics(SearchInfo searchInfo) {
        Map<String, Object> statistics = new HashMap<>();
        statistics.put("orderStatistics", orderMapper.selectOrderStatistics(searchInfo));
        statistics.put("bondStatistics", orderMapper.selectOrderBondStatistics(searchInfo));
        return statistics;
    }

    /**
     * 获取初始查询信息
     *
     * @return 初始查询信息
     */
    @Override
    public Map<String, Object> getInquireInit() {
        Map<String, Object> init = new HashMap<>();
        init.put("merchants", merchantService.selectAllMerchantInfo());
        init.put("siteUrls", merchantService.selectAllMerchantWebSite());
        init.put("exceptionStatus", constantService.getConstantByGroupName(SystemConstant.EXCEPTION_STATUS));
        init.put("acquirerAccounts", acquirerService.selectAllAcquirerAccount());
        init.put("payModes", constantService.getConstantByGroupName(SystemConstant.PAY_MODE));
        init.put("credit", constantService.getConstantByGroupName(SystemConstant.CREDIT));
        init.put("debit", constantService.getConstantByGroupName(SystemConstant.DEBIT));
        init.put("yandex", constantService.getConstantByGroupName(SystemConstant.YANDEX));
        init.put("qiwi", constantService.getConstantByGroupName(SystemConstant.QIWI));
        init.put("remittance", constantService.getConstantByGroupName(SystemConstant.REMITTANCE));
        init.put("bankStatus", constantService.getConstantByGroupName(SystemConstant.BANK_STATUS));
        init.put("shipStatus", constantService.getConstantByGroupName(SystemConstant.SHIP_STATUS));
        init.put("financeStatus", constantService.getConstantByGroupNameAndSortValueIsNotNULL(SystemConstant.FINANCE_STATUS));
        return init;
    }

    @Override
    public Map<String, Object> getMerchantInquirerInit(Integer merchantId) {
        Map<String, Object> init = new HashMap<>();
        init.put("siteUrls", merchantService.selectMerchantWebSiteByMerchantId(merchantId));
        init.put("exceptionStatus", constantService.getConstantByGroupName(SystemConstant.EXCEPTION_STATUS));
        init.put("auditedStatus", constantService.getConstantByGroupName(SystemConstant.AUDITED_STATUS));
        init.put("payModes", constantService.getConstantByGroupName(SystemConstant.PAY_MODE));
        init.put("credit", constantService.getConstantByGroupName(SystemConstant.CREDIT));
        init.put("debit", constantService.getConstantByGroupName(SystemConstant.DEBIT));
        init.put("yandex", constantService.getConstantByGroupName(SystemConstant.YANDEX));
        init.put("qiwi", constantService.getConstantByGroupName(SystemConstant.QIWI));
        init.put("remittance", constantService.getConstantByGroupName(SystemConstant.REMITTANCE));
        init.put("bankStatus", constantService.getConstantByGroupName(SystemConstant.BANK_STATUS));
        init.put("shipStatus", constantService.getConstantByGroupName(SystemConstant.SHIP_STATUS));
        init.put("financeStatus", constantService.getConstantByGroupNameAndSortValueIsNotNULL(SystemConstant.FINANCE_STATUS));
        return init;
    }

    /**
     * 获取拒付初始查询信息
     *
     * @return 初始查询信息
     */
    @Override
    public Map<String, Object> getRefusedInquireInit() {
        Map<String, Object> init = new HashMap<>();
        init.put("merchants", merchantInfoMapper.selectAllIdAndName());
        init.put("siteUrls", merchantWebSiteMapper.selectAllIdAndMerIdAndWeb());
        init.put("country", constantService.getConstantByGroupName(SystemConstant.COUNTRY));
        return init;
    }

    /**
     * 获取订单状态信息
     *
     * @param order 订单对象
     * @return 订单状态信息
     */
    @Override
    public Map<String, Object> getOrderDetail(CoreOrder order) {
        List<CoreOrderGoods> orderGoods = orderGoodsMapper.selectByOrderId(order.getId());
        orderGoods.forEach(coreOrderGoods ->
                coreOrderGoods.setMerchantGoods(merchantGoodsMapper.selectByPrimaryKey(coreOrderGoods.getGoodId())));
        Map<String, Object> detail = new HashMap<>();
        detail.put("orderDetail", orderDetailMapper.selectByOrderId(order.getId()));
        detail.put("cardHolder", cardholderMapper.selectByOrderId(order.getId()));
        detail.put("orderGoods", orderGoods);
        if (new Short(CoreOrder.SHIP_STATUS_SHIPPED).equals(order.getShipStatus())) {
            detail.put("cargoTrack", cargoTrackMapper.selectByOrderId(order.getId()));
        }
        if (new Short(CoreOrder.EXCEPTION_STATUS_NORMAL).equals(order.getExceptionStatus())) {
            detail.put("orderChange", orderChangeMapper.selectDetailByOrderId(order.getId()));
        }
        return detail;
    }

    @Override
    public CoreCargoTrack getOrderCargoTrack(Long orderId) {
        return cargoTrackMapper.selectByOrderId(orderId);
    }

    @Override
    public Map<String, Object> getMerchantOrderDetail(CoreOrder order) {
        Map<String, Object> detail = new HashMap<>();
        detail.put("merchantOrder", merchantOrderMapper.selectBondAndDeductByOrderId(order.getId()));
        detail.put("merchantOrderRmb", merchantOrderRmbMapper.selectBondAndDeductByOrderId(order.getId()));
        detail.put("orderDetail", orderDetailMapper.selectByOrderId(order.getId()));
        detail.put("simpleCard", cardholderMapper.selectSimpleCardByOrderId(order.getId()));
        if (order.getExceptionStatus() != 0) {
            detail.put("orderChange", orderChangeMapper.selectDetailByOrderId(order.getId()));
        }
        return detail;
    }

    /**
     * 获取导出Excel所需信息
     *
     * @param searchInfo 查询条件
     * @return 导出所需信息
     */
    @Override
    public List<VoCoreOrderExport> getExportOrderInfo(SearchInfo searchInfo) {
        if (searchInfo.getEndDate() != null) {
            searchInfo.setEndDate(DateUtils.setEndDate(searchInfo.getEndDate()));
        }
        return orderMapper.selectBySearchInfoForExport(searchInfo);
    }

    /**
     * 批量操作
     *
     * @param orderChanges 变更对象集合
     * @param type         变更类型
     */
    @Override
    public List<CoreOrderChange> batchOperation(List<CoreOrderChange> orderChanges, String type) {
        switch (type) {
            case "批量冻结":
                break;
            case "批量退款":
                refundBatchOperation(orderChanges);
                orderChangeMapper.insertBatch(orderChanges);
                return orderChanges;
            case "批量拒付":
                refusedBatchOperation(orderChanges);
                orderChangeMapper.insertBatch(orderChanges);
                return orderChanges;
            default:
                break;
        }
        return null;
    }

    /**
     * 获取商户订单信息
     *
     * @param orderId 订单号
     * @return 商户订单信息
     */
    @Override
    public CoreMerchantOrder getMerchantOrderByOrderId(Long orderId) {
        return merchantOrderMapper.selectByOrderId(orderId);
    }

    /**
     * 订单变更
     *
     * @param orderChange 变更对象
     * @return 成功/失败
     */
    @Override
    public boolean orderChange(CoreOrderChange orderChange) {
        // 获取订单信息
        CoreOrder order = orderMapper.selectByPrimaryKey(orderChange.getOrderId());
        ChannelOrderInfo channelOrderInfo = channelOrderInfoMapper.selectByOrderId(orderChange.getOrderId());
        setChangeInfo(orderChange, channelOrderInfo, order);
        return orderChangeMapper.insertSelective(orderChange) == 1;
    }

    /**
     * 拒付统计
     *
     * @param searchInfo 查询信息
     * @return 统计信息
     */
    @Override
    public List<Map<String, Object>> getRefusedStatistics(SearchInfo searchInfo) {
        Short types[] = {SystemConstant.FULL_REFUSED, SystemConstant.PARTIAL_REFUSED, SystemConstant.PROXY_REFUSED};
        Short status[] = {SystemConstant.ORDER_CHANGE_STATUS_SUCCESSFUL};
        List<Map<String, Object>> orderSuccessCountInfo = orderMapper.selectCountByStatus(searchInfo, SystemConstant.BANK_STATUS_SUCCESS);
        List<Map<String, Object>> refusedCountInfo = orderChangeMapper.selectCount(types, status, searchInfo);
        if ((orderSuccessCountInfo.size() == 1 && Long.valueOf(orderSuccessCountInfo.get(0).get("count").toString()) == 0) ||
                (refusedCountInfo.size() == 1 && Long.valueOf(refusedCountInfo.get(0).get("refusedCount").toString()) == 0)) {
            return null;
        }
        for (Map<String, Object> rci :
                refusedCountInfo) {
            for (Map<String, Object> oci :
                    orderSuccessCountInfo) {
                if (Integer.parseInt(rci.get("merId").toString()) == Integer.parseInt(oci.get("merId").toString())) {
                    rci.put("count", oci.get("count") == null ? 0L : oci.get("count"));
                    break;
                }
            }
        }
        return refusedCountInfo;
    }

    @Override
    public Integer getOrderCountBySearchInfo(SearchInfo searchInfo) {
        if (searchInfo.getEndDate() != null) {
            searchInfo.setEndDate(DateUtils.setEndDate(searchInfo.getEndDate()));
        }
        return orderMapper.selectCountBySearchInfo(searchInfo);
    }

    @Override
    public Integer getOrderChangeCountBySearchInfo(SearchInfo searchInfo) {
        if (searchInfo.getEndDate() != null) {
            searchInfo.setEndDate(DateUtils.setEndDate(searchInfo.getEndDate()));
        }
        return orderChangeMapper.selectCountBySearchInfo(searchInfo);
    }

    /**
     * 保证金查询
     *
     * @param page 分页信息
     * @return 查询结果
     */
    @Override
    public PageResult getBondInfo(Page page) {
        // 设置分页信息
        PageHelper.startPage(page.getPageNum(), page.getPageSize());
        if (page.getOrderBy() == null || page.getOrderBy().size() == 0) {
            PageHelper.orderBy("co.id DESC");
        } else {
            page.getOrderBy().forEach((key, value) -> PageHelper.orderBy(key + " " + value));
        }
        // 执行查询
        if (page.getSearchInfo() != null && page.getSearchInfo().getEndDate() != null) {
            page.getSearchInfo().setEndDate(DateUtils.setEndDate(page.getSearchInfo().getEndDate()));
        }
        List<VoCoreOrder> list = orderMapper.selectBondInfo(page.getSearchInfo());
        PageInfo<VoCoreOrder> pageInfo = new PageInfo<>(list);
        // 获取查询结果
        PageResult result = new PageResult();
        result.setRows(list);
        result.setTotal(pageInfo.getTotal());
        result.setAllPage(pageInfo.getPages());
        // 返回结果
        return result;
    }

    @Override
    public Map<String, Object> getBondInit() {
        Map<String, Object> init = new HashMap<>();
        init.put("merchants", merchantService.selectAllMerchantInfo());
        init.put("siteUrls", merchantService.selectAllMerchantWebSite());
        init.put("bondStatus", constantService.getConstantByGroupName(SystemConstant.BOND_STATUS));
        return init;
    }

    @Override
    public Map<String, Object> getMerchantBondInit(Integer merchantId) {
        Map<String, Object> init = new HashMap<>();
        init.put("siteUrls", merchantService.selectMerchantWebSiteByMerchantId(merchantId));
        init.put("bondStatus", constantService.getConstantByGroupName(SystemConstant.BOND_STATUS));
        return init;
    }

    @Override
    public List<VoCoreOrder> getExportBondInfo(SearchInfo searchInfo) {
        return orderMapper.selectBondInfoForExport(searchInfo);
    }

    @Override
    public PageResult getMerchantOrderChangeList(Page page) {
        // 设置分页信息
        PageHelper.startPage(page.getPageNum(), page.getPageSize());
        if (page.getOrderBy() == null || page.getOrderBy().size() == 0) {
            PageHelper.orderBy("coc.id DESC");
        } else {
            page.getOrderBy().forEach((key, value) -> PageHelper.orderBy(key + " " + value));
        }
        // 执行查询
        if (page.getSearchInfo() != null && page.getSearchInfo().getEndDate() != null) {
            page.getSearchInfo().setEndDate(DateUtils.setEndDate(page.getSearchInfo().getEndDate()));
        }
        List<VoCoreOrderChange> list = orderChangeMapper.selectMerchantOrderChangeBySearchInfo(page.getSearchInfo());
        PageInfo<VoCoreOrderChange> pageInfo = new PageInfo<>(list);
        // 获取查询结果
        PageResult result = new PageResult();
        result.setRows(list);
        result.setTotal(pageInfo.getTotal());
        result.setAllPage(pageInfo.getPages());
        // 返回结果
        return result;
    }

    @Override
    public List<VoCoreOrderChange> getExportOrderChangeInfo(SearchInfo searchInfo) {
        return orderChangeMapper.selectMerchantOrderChangeForExport(searchInfo);
    }

    /**
     * 设置订单变更信息
     *
     * @param orderChange   变更对象
     * @param channelOrderInfo   通道信息对象
     * @param order   平台订单对象
     */
    private void setChangeInfo(CoreOrderChange orderChange, ChannelOrderInfo channelOrderInfo, CoreOrder order) {
        MerchantSetting merchantSetting = merchantSettingMapper.selectByPrimaryKey(order.getMerId());
        BigDecimal rmbFee = new BigDecimal(orderChange.getType().equals(CoreOrderChange.REFUND) ?
                merchantSetting.getRefundFee() : merchantSetting.getDishonor());
        orderChange.setCurrency(order.getCurrency());
        orderChange.setStatus(CoreOrderChange.STATUS_UNAUDITED);
        orderChange.setId(UUID.createKey("core_order_change"));
        orderChange.setApplicationTime(new Date());
        orderChange.setFee(rmbFee.divide(order.getRate(), 2, BigDecimal.ROUND_HALF_UP));
        orderChange.setRmbFee(rmbFee);
        orderChange.setRmbAmount(orderChange.getAmount().multiply(order.getRate()));
        orderChange.setChangeRate(order.getRate());
        switch (orderChange.getType()) {
            case CoreOrderChange.REFUND:
                if (orderChange.getAmount().compareTo(order.getAmount()) == 0) {
                    orderChange.setType(CoreOrderChange.TYPE_FULL_REFUND);
                } else {
                    orderChange.setType(CoreOrderChange.TYPE_PARTIAL_REFUND);
                }
                break;
            case CoreOrderChange.REFUSED:
                if (orderChange.getAmount().compareTo(order.getAmount()) == 0) {
                    orderChange.setType(CoreOrderChange.TYPE_FULL_REFUSED);
                } else {
                    orderChange.setType(CoreOrderChange.TYPE_PARTIAL_REFUSED);
                }
                break;
            default:
                break;
        }
    }

    /**
     * 拒付批量操作
     *
     * @param orderChanges 变更对象集合
     */
    private void refusedBatchOperation(List<CoreOrderChange> orderChanges) {
        Map<Long, ChannelOrderInfo> channelOrderInfoMap = channelOrderInfoMapper.selectByOrderChangesForOperation(orderChanges);
        for (CoreOrderChange oc :
                orderChanges) {
            // 获取订单信息
            CoreOrder order = orderMapper.selectByPrimaryKey(oc.getOrderId());
            ChannelOrderInfo channelOrderInfo = channelOrderInfoMap.get(oc.getOrderId());
            oc.setAmount(order.getAmount()
                    .subtract(order.getExceptionAmount() == null ?
                            new BigDecimal(0) : order.getExceptionAmount()));
            setChangeInfo(oc, channelOrderInfo, order);
        }
    }

    /**
     * 退款批量操作
     *
     * @param orderChanges 变更对象集合
     */
    private void refundBatchOperation(List<CoreOrderChange> orderChanges) {
        Map<Long, ChannelOrderInfo> channelOrderInfoMap = channelOrderInfoMapper.selectByOrderChangesForOperation(orderChanges);
        for (CoreOrderChange oc :
                orderChanges) {
            // 获取订单信息
            CoreOrder order = orderMapper.selectByPrimaryKey(oc.getOrderId());
            ChannelOrderInfo channelOrderInfo = channelOrderInfoMap.get(oc.getOrderId());
            setChangeInfo(oc, channelOrderInfo, order);
        }
    }

	@Override
	public Map<String, Object> getOrderTrackInit() {

		Map<String, Object> resultMap = new HashMap<String, Object>();
		resultMap.put("merchants", merchantInfoMapper.selectAllIdAndName());
		resultMap.put("payStatusList", constantMapper.selectByGroupName("PayStatus"));

		return resultMap;

	}

	@Override
	public Map<String, Object> getOrderTrackInfo(Map<String, Object> paramMap) {

		Map<String, Object> resultMap = new HashMap<String,Object>();
		Map<String,Object> searchInfoMap  = (Map<String,Object>) paramMap.get("searchInfo");
		if(null != searchInfoMap){

			paramMap.put("merId", searchInfoMap.get("merId"));
			paramMap.put("merOrderId", searchInfoMap.get("merOrderId"));
			paramMap.put("status", searchInfoMap.get("status"));
			paramMap.put("refer", searchInfoMap.get("refer"));
			paramMap.put("minTradeTime", searchInfoMap.get("minTradeTime"));
			paramMap.put("maxTradeTime", searchInfoMap.get("maxTradeTime"));

		}
		paramMap.put("sortFieldName",paramMap.get("sortFieldName"));
		paramMap.put("sortType", paramMap.get("sortType"));
		paramMap.put("pageFirst", paramMap.get("pageFirst"));
		paramMap.put("pageSize", paramMap.get("pageSize"));
		List<VoRiskOrderTrack> orderTrackList = riskOrderTrackMapper.queryListXZM(paramMap);
		int resultTotal = riskOrderTrackMapper.getOrderTrackCountByParam(paramMap);
		for(VoRiskOrderTrack riskOrderTrack : orderTrackList){
			if(null != riskOrderTrack.getSiteId()){

				MerchantWebSite merchantWebSite = merchantWebSiteMapper.selectByPrimaryKey(riskOrderTrack.getSiteId());
				if(null != merchantWebSite){

					riskOrderTrack.setSiteUrl(merchantWebSite.getSiteUrl());


				}else{

					riskOrderTrack.setSiteUrl(riskOrderTrack.getSiteId() + "");

				}

			}

			if(null != riskOrderTrack.getMerId()){

				MerchantInfo merchantInfo = merchantInfoMapper.selectByPrimaryKey(riskOrderTrack.getMerId());
				if(null != merchantInfo){

					riskOrderTrack.setMerName(merchantInfo.getName());


				}else{

					riskOrderTrack.setMerName(riskOrderTrack.getMerId() + "");


				}


			}



		}

		resultMap.put("orderTrackList", orderTrackList);
		resultMap.put("resultTotal", resultTotal);

		return resultMap;
	}

	@Override
	public Map<String, Object> getOrderChangeInit() {

        Map<String, Object> resultMap = new HashMap<String, Object>();
        resultMap.put("merchants", merchantInfoMapper.selectAllIdAndName());
        resultMap.put("siteUrls", merchantWebSiteMapper.selectAllIdAndMerIdAndWeb());
        resultMap.put("exceptionStatusList", constantMapper.selectByGroupName(SystemConstant.EXCEPTION_STATUS));
        resultMap.put("acquirerAccounts", acquirerAccountMapper.selectAllIdAndName());
        return resultMap;


	}

	@Override
	public Map<String, Object> getOrderChangeInfo(Map<String, Object> paramMap) {

		Map<String, Object> resultMap = new HashMap<String,Object>();
		Map<String,Object> searchInfoMap  = (Map<String,Object>) paramMap.get("searchInfo");
		if(null != searchInfoMap){

			paramMap.put("orderId", searchInfoMap.get("orderId"));
			paramMap.put("merOrderId", searchInfoMap.get("merOrderId"));
			paramMap.put("bankOrderId", searchInfoMap.get("bankOrderId"));
			paramMap.put("merId", searchInfoMap.get("merId"));
			paramMap.put("siteId", searchInfoMap.get("siteId"));
			paramMap.put("accountId", searchInfoMap.get("accountId"));
			paramMap.put("applicant", searchInfoMap.get("applicant"));
			paramMap.put("auditor", searchInfoMap.get("auditor"));
			paramMap.put("type", searchInfoMap.get("type"));
			paramMap.put("auditBeginDate", searchInfoMap.get("auditBeginDate"));
			paramMap.put("auditEndDate", searchInfoMap.get("auditEndDate"));

		}
		paramMap.put("sortFieldName",paramMap.get("sortFieldName"));
		paramMap.put("sortType", paramMap.get("sortType"));
		paramMap.put("pageFirst", paramMap.get("pageFirst"));
		paramMap.put("pageSize", paramMap.get("pageSize"));
		List<VoCoreOrderChange> orderChangeList = orderChangeMapper.queryOrderChangeByParam(paramMap);
		int resultTotal = orderChangeMapper.getOrderChangeCountByParam(paramMap);
		List<Map<String, String>> orderChangeStatistics = orderChangeMapper.queryOrderChangeStatistics(paramMap);

		resultMap.put("orderChangeList", orderChangeList);
		resultMap.put("resultTotal", resultTotal);
		resultMap.put("orderChangeStatistics", orderChangeStatistics);

		return resultMap;

	}

    @Override
    public BigDecimal getOrderRmbAmountBySearchInfo(SearchInfo searchInfo) {
        return orderMapper.selectRmbAmountBySearchInfo(searchInfo);
    }

    @Override
    public BigDecimal getOrderChangeRmbAmountBySearchInfo(SearchInfo searchInfo) {
        return orderChangeMapper.selectRmbAmountBySearchInfo(searchInfo);
    }

    public CoreOrder getCoreOrderByOrderId(Long OrderId) {
        return orderMapper.selectByPrimaryKey(OrderId);
    }

    public CoreOrderChange getCoreOrderChangeById(Long OrderChangeId) {
        return orderChangeMapper.selectByPrimaryKey(OrderChangeId);
    }

    public CoreMerchantOrderRmb getALLCoreMerchantOrderRmbById(Long orderId) {
        return merchantOrderRmbMapper.getALLCoreMerchantOrderRmbById(orderId);
    }

    public int updateCoreMerchantOrderRmb(CoreMerchantOrderRmb coreMerchantOrderRmb){
        return merchantOrderRmbMapper.updateByPrimaryKeySelective(coreMerchantOrderRmb);
    }

    public int updateCoreMerchantOrderId(Long newId,Long oldId) {
        return orderChangeMapper.updateCoreMerchantOrderId(newId,oldId);
    }

    public int updateCoreOrderByEntity(CoreOrder coreOrder) {
        return orderMapper.updateByPrimaryKeySelective(coreOrder);
    }

    public int updateCoreOrderChangeByEntity(CoreOrderChange coreOrderChange) {
        return orderChangeMapper.updateByPrimaryKeySelective(coreOrderChange);
    }

    public int updateByPrimaryKeySelectiveByOrderId(CoreMerchantOrder coreMerchantOrder) {
        return merchantOrderMapper.updateByPrimaryKeySelectiveByOrderId(coreMerchantOrder);
    }

}